	package oaSystem.web.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import oaSystem.biz.DepartmentBiz;
import oaSystem.biz.EmployeeBiz;
import oaSystem.biz.RoleBiz;
import oaSystem.entity.Department;
import oaSystem.entity.Employee;
import oaSystem.entity.Role;
import oaSystem.web.security.Authorize;

@Controller
public class EmployeeController {
	
	@Autowired
	private EmployeeBiz employeeBiz;
	@Autowired
	private DepartmentBiz departmentBiz;
	@Autowired
	private RoleBiz roleBiz;
	
	/**
	 * 经理看的
	 * @param model
	 * @param session
	 * @param nr
	 * @param name
	 * @param pageNum
	 * @return
	 */
	@Authorize(rules="employee-search")
	@RequestMapping(value="/employee-search",method=RequestMethod.GET)
	public String searchEmployee(Model model,
					HttpSession session,
					@RequestParam(value="nr",defaultValue="") String nr,
					@RequestParam(value="name",defaultValue="") String name,
					@RequestParam(value="pageNum",defaultValue="1")int pageNum){
		Employee manager = (Employee)session.getAttribute("user");
		int deptId = manager.getDepartment().getId();
		int totalCount = employeeBiz.getEmployeeCount(deptId, name, nr);
		int pageSize = 6;
		List<Employee> employeeList = employeeBiz.getEmployeeByDepartmentIdAndSearch(deptId, pageNum, pageSize, name, nr);
		int totalPage = totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
		model.addAttribute("employeeList",employeeList);
		model.addAttribute("pageNum",pageNum);
		model.addAttribute("totalPage",totalPage);
		model.addAttribute("nrSearch",nr);
		model.addAttribute("nameSearch",name);
		return "employee-searchPage";
	}
	
	/**
	 * 超级管理员看的
	 * @param model
	 * @param session
	 * @param nr
	 * @param name
	 * @param pageNum
	 * @return
	 */
	@Authorize(rules="employee-allsearch")
	@RequestMapping(value="/employee-allsearch",method=RequestMethod.GET)
	public String searchAllEmployee(Model model,
					@RequestParam(value="deptId",defaultValue="0") Integer deptId,
					@RequestParam(value="nr",defaultValue="") String nr,
					@RequestParam(value="name",defaultValue="") String name,
					@RequestParam(value="pageNum",defaultValue="1")int pageNum){
		int totalCount = employeeBiz.getEmployeeCount(deptId, name, nr);
		int pageSize = 6;
		List<Employee> employeeList = employeeBiz.getEmployeeByDepartmentIdAndSearch(deptId, pageNum, pageSize, name, nr);
		int totalPage = totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
		List<Department> allDept = departmentBiz.findAllDepartment();
		model.addAttribute("allDept", allDept);
		model.addAttribute("employeeList",employeeList);
		model.addAttribute("pageNum",pageNum);
		model.addAttribute("totalPage",totalPage);
		model.addAttribute("nrSearch",nr);
		model.addAttribute("nameSearch",name);
		model.addAttribute("deptId",deptId);
		model.addAttribute("all", true);//可以查看全部
		return "employee-searchPage";
	}
	
	/**
	 * 查看员工详情
	 * @param model
	 * @param session
	 * @param nr
	 * @param name
	 * @param pageNum
	 * @return
	 */
	@Authorize(rules="employee-detail")
	@RequestMapping(value="/employee-info")
	public String employee_editPage(Model model,
			@RequestParam(value="id",defaultValue="0") Integer id,
			Integer onlyread){
		//装配描述所有权限的map
		List<Role> allRoleList = roleBiz.findAllRole();
		Map<Role, Boolean> allRoleMap = new HashMap<Role, Boolean>();//所有权限的哈希
		for (Role r : allRoleList) {
			allRoleMap.put(r, false);//1~8 全
		}
		if(id>0){//编辑时
			//推送员工资料
			Employee employee = employeeBiz.fetchEmployeeById(id);
			model.addAttribute("employee", employee);
			//装配描述员工权限的map
			List<Role> eRoleList = employee.getRoles();
			for (Map.Entry<Role, Boolean> ar : allRoleMap.entrySet()) {
				for (Role r : eRoleList) {
					if(ar.getKey().getId()==r.getId()){//筛选
						ar.setValue(true);
					}
				}
			}
		}
		model.addAttribute("departmentList", departmentBiz.findAllDepartment());
		model.addAttribute("onlyread",1);
		model.addAttribute("allRoleMap",allRoleMap);
		return "employee-info";
	}
	
	
	/**
	 * 编辑员工（GET）
	 * @param id
	 * @param model
	 * @return
	 */
	//@Authorize(rules="employee-edit")
	@RequestMapping(value="/employee-edit",method=RequestMethod.GET)
	public String employee_edit(@RequestParam(value="id",defaultValue="0") Integer id,Model model){
		//装配描述所有权限的map
		List<Role> allRoleList = roleBiz.findAllRole();
		Map<Role, Boolean> allRoleMap = new HashMap<Role, Boolean>();//所有权限的哈希
		for (Role r : allRoleList) {
			allRoleMap.put(r, false);//1~8 全
		}
		if(id>0){//编辑时
			//推送员工资料
			Employee employee = employeeBiz.fetchEmployeeById(id);
			model.addAttribute("employee", employee);
			//装配描述员工权限的map
			List<Role> eRoleList = employee.getRoles();
			for (Map.Entry<Role, Boolean> ar : allRoleMap.entrySet()) {
				for (Role r : eRoleList) {
					if(ar.getKey().getId()==r.getId()){//筛选
						ar.setValue(true);
					}
				}
			}
		}
		model.addAttribute("onlyread",0);
		model.addAttribute("departmentList", departmentBiz.findAllDepartment());
		model.addAttribute("allRoleMap",allRoleMap);
		return "employee-info";
	}
	
	/**
	 * 添加员工（Get方法）
	 * @param model
	 * @return
	 */
	@Authorize(rules="employee-add")
	@RequestMapping(value="/employee-add",method=RequestMethod.GET)
	public String employee_add(Model model){
		//装配描述所有权限的map
		List<Role> allRoleList = roleBiz.findAllRole();
		Map<Role, Boolean> allRoleMap = new HashMap<Role, Boolean>();//所有权限的哈希
		for (Role r : allRoleList) {
			allRoleMap.put(r, false);//1~8 全
		}
		model.addAttribute("allRoleMap",allRoleMap);
		model.addAttribute("departmentList", departmentBiz.findAllDepartment());
		return "employee-info";
	}
	
	
	//提交update
	@Authorize(rules="employee-edit")
	@RequestMapping(value="/employee-edit",method=RequestMethod.POST)
	public String employee_edit(Employee employee){
		List<Role> roles = employee.getRoles();
		commitCheckboxFilter(roles);
		employee.setRoles(roles);
		employeeBiz.updateEmployee(employee);	
		return "redirect:/employee-allsearch?parentId=10&deptId="+employee.getDepartment().getId();//重定向  做个简单的分类回显
	}
	
	private void commitCheckboxFilter(List<Role> roles){
		Iterator<Role> iterator = roles.iterator();
		while(iterator.hasNext()){
			Integer roleId = iterator.next().getId();
			if(roleId==null || roleId<=0)
				iterator.remove();
		}
	}
	
	//提交add
	@Authorize(rules="employee-add")
	@RequestMapping(value="/employee-add",method=RequestMethod.POST)
	public String employee_add(Employee employee){
		List<Role> roles = employee.getRoles();
		commitCheckboxFilter(roles);
		employee.setRoles(roles);
		employee.setStatus(1);
		employee.setIsManager(false);
		employeeBiz.addEmployee(employee);
		return "redirect:/employee-allsearch?parentId=10&deptId="+employee.getDepartment().getId();//重定向  做个简单的分类回显
	}
	
	//注销
	@Authorize(rules="employee-logout")
	@RequestMapping(value="/employee-logout",method=RequestMethod.POST)
	public String employee_logout(int id){
		employeeBiz.logOutEmployee(id);
		Employee employee = employeeBiz.fetchEmployeeById(id);
		return "redirect:/employee-allsearch?parentId=10&deptId="+employee.getDepartment().getId();//重定向  做个简单的分类回显
	}
}
